/**** THIS FILE CONSTRUCTS THE TABLES IN THE PAPER - Individual Agency in Street-Level Bureaucrats’ Implementation of Policy Reforms: 
The Role of their Policy Evaluation and Self-Efficacy ****/
/***** THIS FILE MAKES USE OF THE USER-WRITTEN PACKAGES

reghdfe
estout 

AND THEIR ASSOCIATED PACKAGES.

TO INSTALL THEM, PLEASE USE

scc reghdfe, install
scc estout, install
***/

/** THIS LOADS THE DATA CONSTRUCTED IN THE FILE ZZZZ ***/
cd "datapathwheredatahasbeenplaced"
clear

/******** THIS STEP CREATES INFORMATION FROM THE LEADER-SURVEY ***/
use leder_2014.dta, clear
gen year=2014
keep instnr year  lq94 l lq96 lq97 lq02b
append using leder_2015.dta
gen year=2015 if year==.
keep instnr year lq94 l lq96 lq97 lq02b
append using leder_2016.dta
gen year=2016 if year==.
keep instnr year lq94 l lq96 lq97 lq02b
append using leder_2017.dta
gen year=2017 if year==.
keep instnr year lq94 l lq96 lq97 lq02b
append using leder_2018.dta
gen year=2018 if year==.
keep instnr year lq94 l lq96 lq97 lq02b


gen led_aar=lq02b
gen aarsomleder=year-led_aar
 
lab var aarsomleder "No. of years as leader"
recode lq94 lq96 lq97  (1 =1 "Educated at level") (2 3 = 0 "Not educated at level or in training") (8 88=.), pre(rec) label(new_led_udd)

save lederindeks.dta, replace


/***** THIS STEP READS IN AND MERGES THE DATA USED FOR THE ANALYSIS ***/
use datapathwheredatahasbeenplaced\samletelskl.dta, clear
merge 1:1 instnr year using datapathwheredatahasbeenplaced\lederindeks.dta

drop _merge
merge 1:m instnr year using datapathwheredatahasbeenplaced\lpdata.dta

keep if _merge==3


capture tab lpq05, gen(lpq05_)
est clear



est clear


/*** THIS STEP MAKES SURE TAHT WE AREE USING THE RIGHT OVERALL MEASURE OF IMPLEENTATION ***/
egen mean_samlet=rowmean(varsk faelles laering uddiff) if z_varsk!=. & z_faelles!=. & z_laering!=. & z_uddiff!=.
label var mean_samlet "Overall implementation"

/****** FIXED EFFECTS ANALYSIS ****/


/** THIS STEP CLEARS ANY PROBLEMS WITH DANISH LETTERS Æ, Ø and Å ****/
foreach i of varlist _all {
local a: variable label `i'
local a: subinstr local a "Ã¦" "æ"
label var `i' "`a'"
}


clonevar faelles = faellesmaal
clonevar laering = laeringsmal

clonevar comi2 = commitny2

/*** THIS PRODUCES THE RESULTS SHOWN IN TABLE 3 ****/
  foreach var of varlist mean_samlet varsk  faelles laering uddiff {
 reghdfe `var' self  i.year ///
 ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9  male alder antboernf ///
  andenetnisk  antalelever ///
reclq94 reclq96 reclq97 aarsomleder , absorb(panellaerer) cluster(panellaerer)
gen med`var'=1 if e(sample)==1
local label : variable label `var'
est sto stor`var', title("Estimater for `label' (outcome)")
}

esttab stor* using datapathwheredatahasbeenplaced\results.rtf , cells("b(fmt(3))" p(par(`"["' `"]"') fmt(3))) stats(r2_a N, fmt(%12.3f %12.0f) labels("Adjusted R-Squared" "Observations" )) nostar obslast label mtitle  replace dmarker(.) /*drop(_cons)*/  
/*indicate("Controls for school composition=ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9 ses_kat10 male alder antboernf  andenetnisk" /*"Teacher Characteristics=lpq05_2 lpq05_3 lpq01  "*/ "Manager characteristics= reclq94 reclq96 reclq97 aarsomleder" "School resources=antalelever" /*"Year fixed effects=*year*"*/)*/

cap est clear 

/*** THIS PRODUCES THE RESULTS SHOWN IN APPENDIX A2 ***/
foreach var of varlist mean mean_samlet comi2 varsk  faelles laering uddiff self  ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9  male alder antboernf   andenetnisk  antalelever reclq94 reclq96 reclq97 aarsomleder {
	mean `var' if medvarsk==1 
}



/*********THIS PRODUCES THE RESULTS IN TABLE 2 ***/
  foreach var of varlist mean_samlet varsk  faelles laering uddiff  {
 reghdfe `var' comi2 i.year ///
 ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9  male alder antboernf ///
  andenetnisk  antalelever ///
reclq94  reclq96 reclq97 aarsomleder if medvarsk==1, absorb(panellaerer) cluster(panellaerer)
local label : variable label `var'
est sto stor`var', title("Estimater for `label' (outcome)")
}

esttab stor* using datapathwheredatahasbeenplaced\results.rtf , cells("b(fmt(3))" p(par(`"["' `"]"') fmt(3))) stats(r2_a N, fmt(%12.3f %12.0f) labels("Adjusted R-Squared" "Observations" )) nostar obslast label mtitle  append dmarker(.)  
/*indicate("Controls for school composition=ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9 ses_kat10 male alder antboernf  andenetnisk"  "Manager characteristics= reclq94 reclq96 reclq97 aarsomleder " "School resources=antalelever")*/


/***** THIS TABLE PRODUCES THE RESULTS FOR TABLE 4 ***/
cap est clear 
  foreach var of varlist mean_samlet varsk faelles laering uddiff    {
 reghdfe `var'  comi2 self  i.year ///
 ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9 male alder antboernf ///
   andenetnisk  antalelever ///
reclq94  reclq96 reclq97 aarsomleder , absorb(panellaerer) cluster(panellaerer)
local label : variable label `var'
est sto stor`var', title("Estimater for `label' (outcome)")
}

esttab stor* using datapathwheredatahasbeenplaced\results.rtf , cells("b(fmt(3))"  p(par(`"["' `"]"') fmt(3))) stats(r2_a N, fmt(%12.3f %12.0f) labels("Adjusted R-Squared" "Observations" )) nostar obslast label mtitle  append dmarker(.) /*drop(_cons)*/ /*indicate("Controls for school composition=ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9 ses_kat10 male alder antboernf  andenetnisk" /*"Teacher Characteristics=lpq05_2 lpq05_3 lpq01 "*/ "Manager characteristics=reclq94 reclq96 reclq97 aarsomleder  " "School resources=antalelever" "Year fixed effects=*year*")*/


/***** THIS TABLE PRODUCES THE RESULTS FOR TABLE 5 ***/
cap est clear 
cap erase datapathwheredatahasbeenplaced\results_ints.rtf
  foreach var of varlist mean_samlet varsk  faelles laering uddiff l196   {
 reghdfe `var' c.comi2##c.self  i.year ///
 ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9  male alder antboernf ///
   andenetnisk  antalelever ///
reclq94 reclq96 reclq97 aarsomleder , absorb(panellaerer) cluster(panellaerer)
local label : variable label `var'
est sto stor`var', title("Estimater for `label' (outcome)")
}

esttab stor* using datapathwheredatahasbeenplaced\results_ints.rtf , cells("b(fmt(3))" p(par(`"["' `"]"') fmt(3))) stats(r2_a N, fmt(%12.3f %12.0f) labels("Adjusted R-Squared" "Observations" )) nostar obslast label mtitle  append dmarker(.)  
/*indicate("Controls for school composition=ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat6 ses_kat7 ses_kat8 ses_kat9 ses_kat10 male alder antboernf  andenetnisk"  "Manager characteristics=reclq94 reclq96 reclq97 aarsomleder  " "School resources=antalelever" "Year fixed effects=*year*")*/


/*************** THIS TABLE PRODUCES THE RESULTS FOR APPENDIX TABLE 1 ******/
gen medipanel=0
replace medipanel=1 if medvarsk==1

gen miss_aarsomleder =(aarsomleder==.)
replace aarsomleder =0 if aarsomleder ==.


gen miss_reclq97  =( reclq97 ==.)
replace  reclq97  =0 if  reclq97  ==.

gen miss_reclq96  =( reclq96 ==.)
replace  reclq96  =0 if  reclq96  ==.

gen miss_reclq94 =( reclq94 ==.)
replace  reclq94 =0 if  reclq94  ==.


reg medipanel  ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat5 ses_kat6 ses_kat7 ///
 ses_kat8 ses_kat9 /*ses_kat10*/ male alder antboernf   andenetnisk lpq05_2 ///
 lpq05_3  lpq01 undervisledertantalelever antalelever reclq94 reclq96 reclq97 aarsomleder miss_aarsomleder miss_reclq97 miss_reclq96 miss_reclq94 if tag_l==1, cluster(panellaerer)
 
 est sto m1, title("Prob. of being in sample")
 
 reg medipanel  ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat5 ses_kat6 ses_kat7 ///
 ses_kat8 ses_kat9 /*ses_kat10*/ male alder antboernf   andenetnisk  lpq05_2 ///
 lpq05_3  lpq01 undervisledertantalelever antalelever reclq94 reclq96 reclq97 aarsomleder i.year miss_aarsomleder miss_reclq97 miss_reclq96 miss_reclq94 if tag_l==1, cluster(panellaerer)
  est sto m2, title("Prob. of being in sample")
  
reg medipanel ses_kat1 ses_kat2 ses_kat3 ses_kat4 ses_kat5 ses_kat6 ses_kat7 ///
 ses_kat8 ses_kat9 /*ses_kat10*/ male alder antboernf   andenetnisk   ///
 antalelever reclq94 reclq96 reclq97 aarsomleder i.year miss_aarsomleder miss_reclq97 miss_reclq96 miss_reclq94 if tag_l==1, cluster(panellaerer)
  est sto m3, title("Prob. of being in sample")
  
reg medipanel lpq05_2  lpq05_3  lpq01 undervisledertantalelever reclq94 reclq96 reclq97 aarsomleder i.year miss_aarsomleder miss_reclq97 miss_reclq96 miss_reclq94 if tag_l==1, cluster(panellaerer)
  est sto m4, title("Prob. of being in sample")
 
 esttab m1 m2 m3 m4 using datapathwheredatahasbeenplaced\results.rtf , cells("b(fmt(3))"  p(par(`"["' `"]"') fmt(3))) stats(r2 N, fmt(%12.3f %12.0f) labels("R-Squared" "Observations" )) nostar obslast label mtitle  replace dmarker(.)
